//
//  PR_BaseViewController.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/15.
//

#import "PR_BaseViewController.h"
#import <UIButton+LXMImagePosition.h>

#define MHUDTAG 123456789

@interface PR_BaseViewController ()
@property (nonatomic,strong) UIButton* noDataBtn;

@end

@implementation PR_BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    if (self.hj_NavIsHidden) {
//        [self.navigationController setNavigationBarHidden:YES animated:animated];
//    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    
//    if (self.hj_NavIsHidden) {
//        if ([self hj_pushOrPopIsHidden] == NO) {
//            [self.navigationController setNavigationBarHidden:NO animated:animated];
//        }
//    }
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

///监听push下一个或 pop 上一个，是否隐藏导航栏
- (BOOL)hj_pushOrPopIsHidden {
    NSArray * viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count > 0) {
        PR_BaseViewController * vc = viewcontrollers[viewcontrollers.count - 1];
        return vc.hj_NavIsHidden;
    }
    return NO;
}


#pragma mark - HUD
- (void)showHUD
{
    [self showHUD:@""];
}

- (void)showHUD:(NSString *)string
{
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:self.view];
        progressHud.frame = CGRectMake(0, 0, kSPScreenWidth
                                       , kSPScreenHeight);
        progressHud.tag = MHUDTAG;
      //  progressHud.dimBackground = NO;
        progressHud.label.text = string;
        [progressHud setRemoveFromSuperViewOnHide:YES];
        [progressHud showAnimated:YES];
       // [progressHud show:YES];
        [self.view addSubview:progressHud];
        [self.view bringSubviewToFront:progressHud];
    });
    
}

- (void)hideHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *progressHud = (MBProgressHUD *)[self.view viewWithTag:MHUDTAG];
        if (progressHud) {
            [progressHud setHidden:YES];
            [progressHud removeFromSuperview];
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
}

#pragma mark --显示无数据界面
-(void)showNoDataImage
{
    _noDataBtn =[[UIButton alloc] init];
    // 取消用户交互功能
    _noDataBtn.userInteractionEnabled = NO;
    [_noDataBtn setImage:[UIImage imageNamed:@"dingdan_Null"] forState:UIControlStateNormal];
    [_noDataBtn setTitle:@"您还没有相关内容~" forState:UIControlStateNormal];
    [_noDataBtn setTitleColor:kRGBColor(51, 51, 51) forState:UIControlStateNormal];
    _noDataBtn.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [_noDataBtn setImagePosition:LXMImagePositionTop spacing:3];
    [self.view addSubview:_noDataBtn];
   // _noDataBtn.backgroundColor = [UIColor yellowColor];
    [_noDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.view);
        make.width.height.mas_equalTo(@300);
    }];
    [self.view bringSubviewToFront:_noDataBtn];
}

#pragma mark --移除无数据的界面
-(void)removeNoDataImage{
    if (_noDataBtn) {
        [_noDataBtn removeFromSuperview];
        _noDataBtn = nil;
    }
}

#pragma mark --隐藏导航栏黑线
- (void)hideNavBlackLine:(BOOL)hide {
    // 找到黑线
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    if (hide) {
        navBarHairlineImageView.hidden = YES;
    } else {
        navBarHairlineImageView.hidden = NO;
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

//设置状态栏的背景色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    if (@available(iOS 13.0, *)) {
        static UIView *statusBar = nil;
        if (!statusBar) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
                [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
                statusBar.backgroundColor= color;

            });
        } else {
            statusBar.backgroundColor= color;
        }
    } else {
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor= color;
        }
    }
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
